﻿using DemoPrism.Models;
using DemoPrism.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoPrism.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly ILanguageService _languageService;
        private readonly ITenantService _tenantService;
        private readonly IAuthenticationService _authenticationService;
        private readonly IDialogService _dialogService;

        private string _title = "My Prism Application";
        private bool _isLoggedIn;

        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }

        public bool IsLoggedIn
        {
            get => _isLoggedIn;
            set => SetProperty(ref _isLoggedIn, value);
        }

        public ObservableCollection<Tenant> AvailableTenants => _tenantService.AvailableTenants;
        public Tenant CurrentTenant => _tenantService.CurrentTenant;
        public User CurrentUser => _authenticationService.CurrentUser;

        public DelegateCommand<string> ChangeLanguageCommand { get; }
        public DelegateCommand<string> SwitchTenantCommand { get; }
        public DelegateCommand ChangePasswordCommand { get; }
        public DelegateCommand ActivateEmailCommand { get; }
        public DelegateCommand LoginCommand { get; }
        public DelegateCommand LogoutCommand { get; }

        public MainViewModel(
            ILanguageService languageService,
            ITenantService tenantService,
            IAuthenticationService authenticationService,
            IDialogService dialogService)
        {
            _languageService = languageService;
            _tenantService = tenantService;
            _authenticationService = authenticationService;
            _dialogService = dialogService;

            ChangeLanguageCommand = new DelegateCommand<string>(ChangeLanguage);
            SwitchTenantCommand = new DelegateCommand<string>(SwitchTenant);
            ChangePasswordCommand = new DelegateCommand(ChangePassword);
            ActivateEmailCommand = new DelegateCommand(ActivateEmail, CanActivateEmail);
            LoginCommand = new DelegateCommand(Login);
            LogoutCommand = new DelegateCommand(Logout);

            _authenticationService.UserChanged += OnUserChanged;
            _tenantService.TenantChanged += OnTenantChanged;
        }

        private void ChangeLanguage(string languageCode)
        {
            _languageService.SetLanguage(languageCode);
            // 可以在这里更新界面文本
        }

        private void SwitchTenant(string tenantId)
        {
            _tenantService.SwitchTenant(tenantId);
        }

        private void ChangePassword()
        {
            // 显示修改密码对话框
            _dialogService.ShowDialog("ChangePasswordDialog");
        }

        private bool CanActivateEmail()
        {
            return _authenticationService.IsAuthenticated &&
                   _authenticationService.CurrentUser != null &&
                   !_authenticationService.CurrentUser.IsEmailVerified;
        }

        private void ActivateEmail()
        {
            // 显示邮箱激活对话框
            _dialogService.ShowDialog("EmailVerificationDialog");
        }

        private void Login()
        {
            // 显示登录对话框
            _dialogService.ShowDialog("LoginDialog", result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    IsLoggedIn = true;
                }
            });
        }

        private async void Logout()
        {
            await _authenticationService.LogoutAsync();
            IsLoggedIn = false;
        }

        private void OnUserChanged(User user)
        {
            RaisePropertyChanged(nameof(CurrentUser));
            ActivateEmailCommand.RaiseCanExecuteChanged();
        }

        private void OnTenantChanged(Tenant tenant)
        {
            RaisePropertyChanged(nameof(CurrentTenant));
        }
    }
}
